package com.umeox.watch.moto.dataservice.stub;

import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.umeox.moto.common.log.Logger;
import com.umeox.watch.moto.apiclient.model.AgentAppConfigDTO;
import com.umeox.watch.moto.apiclient.model.AgentUsedRecord;
import com.umeox.watch.moto.apiclient.model.ChantsCountDTO;
import com.umeox.watch.moto.apiclient.model.ChantsCounterDTO;
import com.umeox.watch.moto.apiclient.model.GetFriendsAttrDTO;
import com.umeox.watch.moto.apiclient.model.StepRankingDTO;
import com.umeox.watch.moto.apiclient.model.StoryDTO;
import com.umeox.watch.moto.apiclient.model.TaskRecordInfoDTO;
import com.umeox.watch.moto.apiclient.model.UserAgentDTO;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.base.BaseApp;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.dataservice.db.AppDataBase;
import com.umeox.watch.moto.dataservice.interfaces.IPushApiClient;
import com.umeox.watch.moto.dataservice.services.ApiService;

import net.vidageek.mirror.dsl.Mirror;

import java.util.ArrayList;
import java.util.List;

public class PushApiClientStub extends android.os.Binder implements IPushApiClient {

    public PushApiClientStub() {
        this.attachInterface(this, DESCRIPTOR);
    }

    public static void registerBinder() {
        try {
            new Mirror().on("android.os.ServiceManager").invoke().method("addService").withArgs(BuildConfig.pushApiBinderName, new PushApiClientStub());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder asBinder() {
        return this;
    }

    @Override
    public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws RemoteException {
        String descriptor = DESCRIPTOR;
        switch (code) {
            case INTERFACE_TRANSACTION: {
                reply.writeString(descriptor);
                return true;
            }
            case TRANSACTION_SERVER_CONFIG: {
                data.enforceInterface(descriptor);
                this.serverConfig();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_DEVICE_CONFIG: {
                data.enforceInterface(descriptor);
                this.deviceConfig();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_BIND: {
                data.enforceInterface(descriptor);
                this.bind();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_UNBIND: {
                data.enforceInterface(descriptor);
                this.unbind();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_REQUEST_GET_CONTACTS: {
                data.enforceInterface(descriptor);
                this.requestGetContacts();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_REQUEST_GET_BUDDIES: {
                data.enforceInterface(descriptor);
                this.requestGetBuddies();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_LOCTION_PERIODS_CONFIG: {
                data.enforceInterface(descriptor);
                this.locationPeriodsConfig();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_FORBID_PERIODS_CONFIG: {
                data.enforceInterface(descriptor);
                this.forbidPeriodsConfig();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_REQUEST_GET_ALARM_CLOCKS: {
                data.enforceInterface(descriptor);
                this.requestGetAlarmClocks();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_REQUEST_GET_WIFI_SETTINGS: {
                data.enforceInterface(descriptor);
                this.requestGetWifiSettings();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_REQUEST_GET_WALLPAPERS: {
                data.enforceInterface(descriptor);
                this.requestGetWallpapers();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_REQUEST_GET_STEPINFO: {
                data.enforceInterface(descriptor);
                this.requestGetStepInfo();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_REQUEST_GET_BINDCODE: {
                data.enforceInterface(descriptor);
                this.requestGetBindCode();
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_GET_STORIES_PAGE: {
                data.enforceInterface(descriptor);
                List<StoryDTO> result = getStoriesPage();
                reply.writeNoException();
                reply.writeTypedList(result);
                return true;
            }
            case TRANSACTION_GET_RECOMMEND_STORIES: {
                data.enforceInterface(descriptor);
                List<StoryDTO> result = getRecommendStories();
                reply.writeNoException();
                reply.writeTypedList(result);
                return true;
            }
            case TRANSACTION_GET_STEP_RANKING: {
                data.enforceInterface(descriptor);
                StepRankingDTO result = getStepRanking();
                reply.writeNoException();
                result.writeToParcel(reply, 0);
                return true;
            }
            case TRANSACTION_GET_TASK_RECORD: {
                data.enforceInterface(descriptor);
                TaskRecordInfoDTO result = getTaskRecord();
                reply.writeNoException();
                result.writeToParcel(reply, 0);
                return true;
            }
            case TRANSACTION_GET_CHANTS_COUNTER: {
                data.enforceInterface(descriptor);
                ChantsCounterDTO result = getChantsCounter();
                reply.writeNoException();
                result.writeToParcel(reply, 0);
                return true;
            }
            case TRANSACTION_GET_FRIENDS_ATTR: {
                data.enforceInterface(descriptor);
                GetFriendsAttrDTO result = getFriendsAttr();
                reply.writeNoException();
                result.writeToParcel(reply, 0);
                return true;
            }
            case TRANSACTION_CREATE_GROUP: {
                data.enforceInterface(descriptor);
                String groupName = data.readString();
                List<String> friendIds = new ArrayList<>();
                data.readStringList(friendIds);
                this.createGroup(groupName, friendIds);
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_UPDATE_GROUP: {
                data.enforceInterface(descriptor);
                String groupId = data.readString();
                String groupName = data.readString();
                this.updateGroup(groupId, groupName);
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_INVITE_JOIN_GROUP: {
                data.enforceInterface(descriptor);
                String groupId = data.readString();
                List<String> friendIds = new ArrayList<>();
                data.readStringList(friendIds);
                this.inviteJoinGroup(groupId, friendIds);
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_REMOVE_GROUP_MEMBER: {
                data.enforceInterface(descriptor);
                String groupId = data.readString();
                List<String> friendIds = new ArrayList<>();
                data.readStringList(friendIds);
                this.removeGroupMember(groupId, friendIds);
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_QUIT_GROUP: {
                data.enforceInterface(descriptor);
                String groupId = data.readString();
                this.quitGroup(groupId);
                reply.writeNoException();
                return true;
            }
            case TRANSACTION_GET_AGENT_USER: {
                data.enforceInterface(descriptor);
                String appKey = data.readString();
                String agentId = data.readString();
                String userId = data.readString();
                UserAgentDTO result = getAgentUser(appKey, agentId, userId);
                reply.writeNoException();
                result.writeToParcel(reply, 0);
                return true;
            }
            case TRANSACTION_GET_AGENT_APP_CONFIG: {
                data.enforceInterface(descriptor);
                String appKey = data.readString();
                String userId = data.readString();
                int type = data.readInt();
                AgentAppConfigDTO result = getAgentAppConfig(appKey, userId, type);
                reply.writeNoException();
                result.writeToParcel(reply, 0);
                return true;
            }
            case TRANSACTION_SUBMIT_AGENT_USED_RECORD: {
                data.enforceInterface(descriptor);
                String appKey = data.readString();
                String userId = data.readString();
                List<AgentUsedRecord> recordList = new ArrayList<>();
                data.readTypedList(recordList, AgentUsedRecord.CREATOR);
                this.submitAgentUsedRecord(appKey, userId, recordList);
                reply.writeNoException();
                return true;
            }
            default: {
                return super.onTransact(code, data, reply, flags);
            }
        }
    }

    @Override
    public void serverConfig() {
        ApiService.getServerConfig(BaseApp.getInstance().deviceId);
    }

    @Override
    public void deviceConfig() {
        ApiService.getDeviceConfig();
    }

    @Override
    public void bind() {
        ApiService.getDeviceConfig(BaseApp.getInstance().deviceId, (success, result) -> {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND);
            intent.setAction(Constants.ACTION_ACTIVATION);
            BaseApp.getInstance().sendBroadcast(intent);
            Logger.d("设备已绑定");
        });
    }

    @Override
    public void unbind() {

        Logger.d("删除联系人：" + AppDataBase.getInstance().contactDao().deleteAll());
        Logger.d("删除好友：" + AppDataBase.getInstance().buddyDao().deleteAll());
        Logger.d("删除闹钟：" + AppDataBase.getInstance().alarmDao().deleteAll());
        Logger.d("删除壁纸：" + AppDataBase.getInstance().wallpaperDao().deleteAll());
        Logger.d("删除记步设置：" + AppDataBase.getInstance().stepInfoDao().deleteAll());
        Logger.d("删除定位频率设置：" + AppDataBase.getInstance().locationPeriodDao().deleteAll());
        Logger.d("删除禁用时段设置：" + AppDataBase.getInstance().forbidPeriodDao().deleteAll());
        Logger.d("删除视频通话记录：" + DataProvider.removeVideoCalllog());
        Logger.d("删除位置信息：" + AppDataBase.getInstance().locationInfoDao().deleteAll());
        Logger.d("删除记步信息：" + DataProvider.removePedometer());
        Logger.d("删除任务信息：" + AppDataBase.getInstance().tasksAttributeDao().deleteAll());
        ApiService.getDeviceConfig(BaseApp.getInstance().deviceId, (success, result) -> {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND);
            intent.setAction(Constants.ACTION_UNBIND);
            BaseApp.getInstance().sendBroadcast(intent);
            Logger.d("设备已解绑");
        });
    }

    @Override
    public void requestGetContacts() {

        ApiService.getContacts();

    }

    @Override
    public void requestGetBuddies() {
        ApiService.getBuddies();
    }

    @Override
    public void locationPeriodsConfig() {
        ApiService.getConcernTime(BaseApp.getInstance().deviceId);
    }

    @Override
    public void forbidPeriodsConfig() {
        ApiService.getForbidPeriods(BaseApp.getInstance().deviceId);

    }

    @Override
    public void requestGetAlarmClocks() {
        ApiService.getAlarmList(BaseApp.getInstance().deviceId);
    }

    @Override
    public void requestGetWifiSettings() {
        ApiService.getWifiList(BaseApp.getInstance().deviceId);
    }

    @Override
    public void requestGetWallpapers() {
        ApiService.getWallpapers();
    }

    @Override
    public void requestGetStepInfo() {
        ApiService.getStepInfo(BaseApp.getInstance().deviceId);
    }

    @Override
    public void requestGetBindCode() {
        ApiService.getDeviceCode();
    }

    @Override
    public List<StoryDTO> getStoriesPage() {
        return ApiService.getStoriesPage();
    }

    @Override
    public List<StoryDTO> getRecommendStories() {
        return ApiService.getRecommendStories();
    }

    @Override
    public StepRankingDTO getStepRanking() throws RemoteException {
        return ApiService.getStepRanking();
    }

    @Override
    public TaskRecordInfoDTO getTaskRecord() throws RemoteException {
        return ApiService.getTaskRecord();
    }

    @Override
    public ChantsCounterDTO getChantsCounter() throws RemoteException {
        return ApiService.getChantsCounter();
    }

    @Override
    public GetFriendsAttrDTO getFriendsAttr() throws RemoteException {
        return ApiService.getFriendsAttr();
    }

    @Override
    public void createGroup(String groupName, List<String> friendIds) throws RemoteException {
        ApiService.createGroup(groupName, friendIds);
    }

    @Override
    public void updateGroup(String groupId, String groupName) throws RemoteException {
        ApiService.updateGroup(groupId, groupName);
    }

    @Override
    public void inviteJoinGroup(String groupId, List<String> friendIds) throws RemoteException {
        ApiService.inviteJoinGroup(groupId, friendIds);
    }

    @Override
    public void removeGroupMember(String groupId, List<String> friendIds) throws RemoteException {
        ApiService.removeGroupMember(groupId, friendIds);
    }

    @Override
    public void quitGroup(String groupId) throws RemoteException {
        ApiService.quitGroup(groupId);
    }

    @Override
    public UserAgentDTO getAgentUser(String appKey, String agentId, String userId) throws RemoteException {
        return ApiService.getAgentUser(appKey, agentId, userId);
    }

    @Override
    public AgentAppConfigDTO getAgentAppConfig(String appKey, String userId, int type) throws RemoteException {
        return ApiService.getAgentAppConfig(appKey, userId, type);
    }

    @Override
    public void submitAgentUsedRecord(String appKey, String userId, List<AgentUsedRecord> details) throws RemoteException {
        ApiService.submitAgentUsedRecord(appKey, userId, details);
    }
}
